package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.settlement.system.model.query.${entity}Query;
import com.settlement.system.model.form.${entity}Form;
import org.springdoc.core.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.Parameter;
import com.settlement.system.model.vo.${entity}ImportVo;
import com.settlement.system.listener.easyexcel.${entity}Listener;
import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.EasyExcelFactory;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("/api/v1/${table.entityPath?replace("sys","")?uncap_first}")
@RequiredArgsConstructor
public class ${table.controllerName} {

    private final ${table.serviceName} ${table.serviceName?uncap_first};

    @Operation(summary = "${table.comment}分页", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<${entity}> getPage(@ParameterObject ${entity}Query queryParams) {
        Page<${entity}> result = ${table.serviceName?uncap_first}.getPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "保存${table.comment}", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreventDuplicateSubmit
    @PreAuthorize("@ss.hasPerm('sys:${table.entityPath?replace("sys","")?uncap_first}:save')")
    public Result<?> save${entity}(@RequestBody @Valid ${entity}Form ${entity?uncap_first}Form) {
        boolean result = ${table.serviceName?uncap_first}.save${entity}(${entity?uncap_first}Form);
        return Result.judge(result);
    }

    @Operation(summary = "获取${table.comment}信息", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public Result<${entity}Form> get${entity}FormData(@Parameter(description = "主键ID") @PathVariable Long id) {
        ${entity}Form ${entity?uncap_first} = ${table.serviceName?uncap_first}.get${entity}FormData(id);
        return Result.success(${entity?uncap_first});
    }

    @Operation(summary = "删除${table.comment}", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:${table.entityPath?replace("sys","")?uncap_first}:delete')")
    public Result<?> delete${entity}(@Parameter(description = "ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = ${table.serviceName?uncap_first}.delete${entity}(ids);
        return Result.judge(result);
    }

    @Operation(summary = "${table.comment!}导入", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/${entity?uncap_first}Import")
    @PreAuthorize("@ss.hasPerm('sys:${table.entityPath?replace("sys","")?uncap_first}:import')")
    @PreventDuplicateSubmit
    public Result<?> ${entity?uncap_first}Import(@RequestParam("file") MultipartFile file) throws Exception {
        ${entity}Listener listener = new ${entity}Listener();
        EasyExcelFactory.read(file.getInputStream(), ${entity}ImportVo.class, listener).sheet(0).doRead();
        List<${entity}ImportVo> list = listener.getVos();
        if (list == null || list.size() == 0) {
            return Result.success();
        }
        ${entity?uncap_first}Service.importData(list);
        return Result.success();
    }

    @Operation(summary = "导出${table.comment!}", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:${table.entityPath?replace("sys","")?uncap_first}:export')")
    @PreventDuplicateSubmit
    public void export(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String fileName = "${table.comment!}.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
        List<${entity}ImportVo> ${entity?uncap_first}List = ${entity?uncap_first}Service.findByIds(ids);
        EasyExcel.write(response.getOutputStream(), ${entity}ImportVo.class).sheet("${table.comment!}").doWrite(${entity?uncap_first}List);
    }
}

